package com.zhang.bingmei_incomeexpenditure.until;

import org.springframework.beans.BeanUtils;

public class BeanConvertUtil {
    /**
     * 实体对象转换为VO对象
     * @param source 源实体
     * @param targetClass 目标VO类
     * @return 转换后的VO
     */
    public static <S, T> T convert(S source, Class<T> targetClass) {
        if (source == null) {
            return null;
        }
        try {
            T target = targetClass.getDeclaredConstructor().newInstance();
            BeanUtils.copyProperties(source, target);
            return target;
        } catch (Exception e) {
            throw new RuntimeException("对象转换失败：" + source.getClass().getName() + "→" + targetClass.getName(), e);
        }
    }
}